package top.bingoz.wanandroid.base

import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.support.AndroidSupportInjection
import dagger.android.support.HasSupportFragmentInjector
import top.bingoz.wanandroid.contract.Contract
import top.bingoz.wanandroid.toast
import javax.inject.Inject

/**
 * @author su
 * @date 2018/8/2
 * @description
 */
abstract class BaseMvpFragment : BaseFragment(), Contract.View, HasSupportFragmentInjector {

    @Inject
    lateinit var childFragmentInjector: DispatchingAndroidInjector<Fragment>

    var mPresenter: MultiPresenter = MultiPresenter()

    override fun supportFragmentInjector(): AndroidInjector<Fragment> {
        return childFragmentInjector
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return LayoutInflater.from(container?.context).inflate(getLayoutId(), container, false)
    }

    override fun onAttach(context: Context?) {
        AndroidSupportInjection.inject(this)
        super.onAttach(context)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        bindPresenter(mPresenter)
        mPresenter.attachView(this)
        super.onViewCreated(view, savedInstanceState)
    }

    open protected fun bindPresenter(presenter: MultiPresenter) {
    }


    override fun onDestroyView() {
        super.onDestroyView()
        mPresenter.detachView()
    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter.destroy()
    }


    override fun showloading() {
    }

    override fun dismissloading() {

    }

    override fun showError(msg: String?) {
        context?.toast(msg)
    }

}